import { observable, action } from 'mobx';
import {WeaForm} from 'comsMobx';
import * as API from '../apis/finance';
import * as Util from '../util/index';
import {WeaTableNew} from 'comsMobx'
import { message } from 'antd';
import isEmpty from 'lodash/isEmpty';
const {TableStore} = WeaTableNew;
import {i18n} from '../public/i18n';
import {WeaLocaleProvider} from 'ecCom';
import FinanceExpand from './financeExpand.js';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmFinance {
  loading = false;
  @observable financeExpand = new FinanceExpand();
  
  title = () => getLabel('16416',"我的工资");
  @observable tabDatas = [];
  @observable isEditor = false;
  @observable form = new WeaForm();
  @observable buttons = {};
  @observable formFields = [];
  @observable table = {};
  @observable tabkey = '1';
  @observable hrmId;

  @action
  getTabInfo = (params = {}) => {
    let _this = this;
    params.id=this.hrmId;
    API.getTabInfo(params).then((data) => {
      this.tabDatas = data.tabs;
      if(!isEmpty(data.tabs)){
        this.tabkey = data.tabs[0].key;
      }
      _this.getData(params);
    });
  }

  getData = (params = {}) => {
    API.getFormField(params).then((data) => {
      this.formFields = data.fieldgroup;
      this.buttons = data.buttons;
      // this.hrmId = data.hrmId;
      this.form.initFormFields(this.formFields);
    });
    API.getSearchList({id:this.hrmId,tabkey:this.tabkey}).then((data) => {
      this.table = data.table;
    });
  }

  edit = () => {
    this.formFields && this.formFields.forEach(c =>{
      c.items.forEach( field => {
          field.viewAttr = 2;
          if (field.conditionType == 'BROWSER') {
              field.browserConditionParam.viewAttr = 2;
          }
      });
    });
    this.isEditor = true;
    this.tabkey = '1';
  }

  save = () => {
    if(this.loading) return;
    this.loading = true;
    const formParams = this.form.getFormParams();
    API.saveFormFields({...{id: this.hrmId}, ...formParams}).then((data) => {
      if (data.status == '1') {
        message.success(i18n.message.saveSuccess());
        this.init();
        this.getData({id:this.hrmId});
      } else {
          message.warning(data.message);
      }
      this.loading = false;
    }, error=> {
      message.warning(error.message);
      this.loading = false;
    });
  }

  init = () => {
    this.tabkey = '1';
    this.isEditor = false;
    this.form = new WeaForm();
  }

  changeTab = (key) => {
    this.tabkey = key;
    this.getData();
  }

  getFormParams = () => {
    return this.form.getFormParams();
  }
}